package com.atguigu.service.impl;

import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.Permission;
import com.atguigu.entity.Role;
import com.atguigu.mapper.AdminRoleMapper;
import com.atguigu.mapper.PermissionMapper;
import com.atguigu.mapper.RoleMapper;
import com.atguigu.mapper.RolePermissionMapper;
import com.atguigu.service.RoleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/9/20 20:01
 * @Description:
 */
@DubboService
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public BaseMapper<Role> getBaseMapper() {
        return roleMapper;
    }

    @Override
    public void assignPermission(Long roleId, List<Long> permissionIds) {
        //先删除角色中的权限
        rolePermissionMapper.deletePermissionByRoleId(roleId);
        //重新分配权限
        rolePermissionMapper.assignPermission(roleId,permissionIds);
    }

    @Override
    public List<Map<String,Object>> findPermissionByRoleId(Long roleId) {
        //获取所有权限
        List<Permission> allPermissionList = permissionMapper.findAll();
        //获取用户拥有的权限id
        List<Long> permissionIdList = rolePermissionMapper.findPermissionIdByRoleId(roleId);
        //存储权限
        List<Map<String,Object>> permissionList = new ArrayList<>();
        for (Permission permission : allPermissionList) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",permission.getId());
            map.put("pId",permission.getParentId());
            map.put("name",permission.getName());
            if (permissionIdList.contains(permission.getId())){
                map.put("checked",true);
            }
            permissionList.add(map);
        }
        return permissionList;
    }

    @Override
    public Map<String, List<Role>> findRoleListById(Long adminId) {
        //查询所有角色集合
        List<Role> roleList = roleMapper.findAll();
        //查询用户所拥有的所有的角色的id集合
        List<Long> adminRoleIdList = adminRoleMapper.findRoleListById(adminId);
        //创建用户已分配的角色集合和未分配角色的集合
        List<Role> assignRoleList = new ArrayList<>();
        List<Role> unAssignRoleList = new ArrayList<>();
        //根据查询到的角色的id与用户所拥有的角色id进行判断,然后将所有角色进行分类
        for (Role role : roleList) {
            if (adminRoleIdList.contains(role.getId())){
                assignRoleList.add(role);
            }else {
                unAssignRoleList.add(role);
            }
        }
        Map<String,List<Role>> roleMap = new HashMap<>();
        roleMap.put("assignRoleList",assignRoleList);
        roleMap.put("unAssignRoleList",unAssignRoleList);

        System.out.println(roleMap.toString());

        return roleMap;
    }
}
